package nxu.it.movieapi.controller;

import nxu.it.api.common.param.PageQueryParam;
import nxu.it.api.common.result.ApiPageResult;
import nxu.it.api.common.result.ApiResult;
import nxu.it.api.common.result.Pageable;
import nxu.it.api.model.*;
import nxu.it.api.service.CommentService;
import nxu.it.api.service.GenreService;
import nxu.it.api.service.MovieService;
import nxu.it.api.service.StaffService;
import nxu.it.api.service.param.MoviePageQueryParam;
import nxu.it.api.service.param.StaffPageQueryParam;
import nxu.it.movieapi.service.AuthorService;
import nxu.it.movieapi.service.UserRegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/movie/api/v1")
public class MovieApiV1Controller {
    @Autowired
    private AuthorService authorService;
    @Autowired
    private GenreService genreService;
    @Autowired
    private MovieService movieService;
    @Autowired
    private StaffService staffService;
    @Autowired
    private CommentService commentService;
    @Autowired
    private UserRegisterService userRegisterService;

    @GetMapping("/author")
    public ResponseEntity<ApiResult<List<Author>>> author() {
        List<Author> authorList = authorService.findAll();
        ApiResult<List<Author>> apiResult = ApiResult.success(authorList);
        return ResponseEntity.ok(apiResult);
    }

    @GetMapping("/genres")
    public ResponseEntity<ApiResult<List<Genre>>> genres() {
        List<Genre> genreList = genreService.findAll();
        ApiResult<List<Genre>> apiResult = ApiResult.success(genreList);
        return ResponseEntity.ok(apiResult);
    }

    @GetMapping("/movie/{movieId}")
    public ResponseEntity<ApiResult<Movie>> movie(@NotNull @PathVariable Integer movieId) {
        Optional<Movie> movieOptional = movieService.findById(movieId);
        Optional<ApiResult<Movie>> optionalMovieApiResult = movieOptional.map(ApiResult::success);
        return ResponseEntity.of(optionalMovieApiResult);
    }

    @PostMapping("/movies")
    public ResponseEntity<ApiPageResult<SimpleMovie>> movies(@RequestBody  MoviePageQueryParam moviePageQueryParam) {
        Pageable<SimpleMovie> moviePage = movieService.page(moviePageQueryParam);
        ApiPageResult<SimpleMovie> apiResult = ApiPageResult.success(moviePage);
        return ResponseEntity.ok(apiResult);
    }

    @GetMapping("/staff/{staffId}")
    public ResponseEntity<ApiResult<Staff>> staff(@NotNull @PathVariable Integer staffId) {
        Optional<Staff> staffOptional = staffService.findById(staffId);
        Optional<ApiResult<Staff>> optionalStaffApiResult = staffOptional.map(ApiResult::success);
        return ResponseEntity.of(optionalStaffApiResult);
    }

    @PostMapping("/staffs")
    public ResponseEntity<ApiPageResult<SimpleStaff>> staffs(@RequestBody StaffPageQueryParam staffPageQueryParam) {
        Pageable<SimpleStaff> staffPage = staffService.page(staffPageQueryParam);
        ApiPageResult<SimpleStaff> apiResult = ApiPageResult.success(staffPage);
        return ResponseEntity.ok(apiResult);
    }

    @PostMapping("/movie/{movieId}/comments")
    public ResponseEntity<ApiPageResult<Comment>> movieComments(@NotNull @PathVariable Integer movieId, @RequestBody PageQueryParam pageQueryParam) {
        Pageable<Comment> commentPage = commentService.pageByMovieId(movieId, pageQueryParam);
        ApiPageResult<Comment> apiResult = ApiPageResult.success(commentPage);
        return ResponseEntity.ok(apiResult);
    }

    @PostMapping("/user/{userId}/comments")
    public ResponseEntity<ApiPageResult<Comment>> userComments(@NotNull @PathVariable Integer userId, @RequestBody PageQueryParam pageQueryParam) {
        Pageable<Comment> commentPage = commentService.pageByUserId(userId, pageQueryParam);
        ApiPageResult<Comment> apiResult = ApiPageResult.success(commentPage);
        return ResponseEntity.ok(apiResult);
    }
    @PostMapping("/register")
    public ResponseEntity<ApiResult<User>> register(@RequestBody User user) {
        User registeredUser = userRegisterService.register(user);
        ApiResult<User> apiResult = ApiResult.success(registeredUser);
        return ResponseEntity.ok(apiResult);
    }


}
